package reflect;

import java.lang.reflect.Field;

/**
 * 反射机制操作属性
 */
public class ReflectDemo8 {
    public static void main(String[] args)throws Exception {
        Teacher teacher = new Teacher();
        teacher.name = "张三";//很少直接为属性赋值，都是通过调用get,set方法操作
        System.out.println(teacher);


        Class cls = Class.forName("reflect.Teacher");
        Object obj = cls.newInstance();
        /*
            java.lang.reflect.Field 类
            它的每一个实例用于表示一个属性的信息，也是反射对象之一
         */
        Field field = cls.getDeclaredField("name");//获取Teacher的name属性
        field.set(obj,"李四");//teacher.name = "李四";
        System.out.println(obj);

        Object value = field.get(obj);//Object value = teacher.name;
        System.out.println(value);
    }
}
